package com.lc.railway.ui.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.railway.R;
import com.lc.railway.app.AppFragment;
import com.lc.railway.http.glide.GlideApp;
import com.lc.railway.http.request.ApiNewsListsBean;
import com.lc.railway.http.request.ApinNewsListsApi;
import com.lc.railway.other.IntentKey;
import com.lc.railway.ui.activity.HomeActivity;
import com.lc.railway.ui.activity.NewsDeatailsActivity;
import com.lc.railway.ui.activity.SearchActivity;
import com.lc.railway.util.CourseUtils;
import com.lc.railway.util.SPUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * 教育整顿
 */
public class EducationFragment extends AppFragment<HomeActivity> {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    private BaseQuickAdapter<ApiNewsListsBean.DataBeanX.DataBean, BaseViewHolder> adapter;
    private List<ApiNewsListsBean.DataBeanX.DataBean> datas = new ArrayList<>();
    private int mPage = 1;
    private int total;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_education;
    }

    @Override
    protected void initView() {
        adapter = new BaseQuickAdapter<ApiNewsListsBean.DataBeanX.DataBean, BaseViewHolder>(R.layout.item_test_group, datas) {
            @Override
            protected void convert(@NotNull BaseViewHolder holder, ApiNewsListsBean.DataBeanX.DataBean dataBeanX) {
                holder.setText(R.id.tv_title, dataBeanX.getTitle());
                holder.setText(R.id.tv_date, dataBeanX.getCreate_time());

                if (CourseUtils.isNullOrEmpty(dataBeanX.getPicurl())) {
                    holder.setGone(R.id.img_logo, true);
                } else {
                    holder.setVisible(R.id.img_logo, true);
                }
                GlideApp.with(getContext()).load(dataBeanX.getPicurl()).error(R.drawable.image11).into(((ImageView) holder.getView(R.id.img_logo)));
            }
        };
        recyclerView.setAdapter(adapter);
        View emptyview = View.inflate(getContext(), R.layout.empty_emptylayout, null);
        adapter.setEmptyView(emptyview);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                startActivity(new Intent(getContext(),NewsDeatailsActivity.class).putExtra("id",datas.get(position).getId()+"").putExtra("title",datas.get(position).getTitle()).putExtra("content",datas.get(position).getTitle()).putExtra("picurl",datas.get(position).getPicurl()).putExtra("time",datas.get(position).getCreate_time()));

            }
        });
        api_news_lists(mPage);
        mRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(final @NonNull RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(() -> {
                    mPage++;
                    if (mPage < total) {
                        api_news_listsMore(mPage);
                        refreshLayout.finishLoadMore();
                    } else {
                        toast("数据全部加载完毕");
                        refreshLayout.finishLoadMoreWithNoMoreData();//将不会再次触发加载更多事件
                    }
                }, 1000);
            }

            @Override
            public void onRefresh(final @NonNull RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(() -> {
                    mPage = 1;
                    api_news_lists(mPage);
                    refreshLayout.finishRefresh();
                }, 1000);

            }
        });
    }

    private void api_news_listsMore(int mPage) {
        EasyHttp.post(this)
                .api(new ApinNewsListsApi().setClassid("33").setPage(mPage).setMember_id(SPUtil.get(getContext(), IntentKey.ID,"")))
                .request(new HttpCallback<ApiNewsListsBean>(this) {
                    @Override
                    public void onSucceed(ApiNewsListsBean data) {

                        if (data.getError_code() == 200) {

                            datas.addAll(data.getData().getData());
                            total = data.getData().getTotal();
                            adapter.setNewData(datas);
                            adapter.notifyDataSetChanged();
                        }
                    }
                });
    }

    private void api_news_lists(int page) {
        EasyHttp.post(this)
                .api(new ApinNewsListsApi().setClassid("33").setPage(page).setMember_id(SPUtil.get(getContext(), IntentKey.ID,"")))
                .request(new HttpCallback<ApiNewsListsBean>(this) {
                    @Override
                    public void onSucceed(ApiNewsListsBean data) {

                        if (data.getError_code() == 200) {

                            datas.clear();
                            datas.addAll(data.getData().getData());
                            total = data.getData().getTotal();
                            adapter.setNewData(datas);
                            adapter.notifyDataSetChanged();
                        }
                    }
                });


    }

    @Override
    protected void initData() {

    }
    @OnClick(R.id.iv_home_search)
    public void onClick() {
        startActivity(SearchActivity.class);
    }
}
